import {Component, OnInit} from '@angular/core';
import {FormBuilder, FormGroup, Validators} from '@angular/forms';
import {Router} from '@angular/router';
import {timer} from 'rxjs';
import {AppService} from '../../service/app/app.service';

@Component({
  selector: 'app-rest-password',
  templateUrl: './rest-password.component.html',
  styleUrls: ['./rest-password.component.css']
})
export class RestPasswordComponent implements OnInit {
  validateForm: FormGroup;
  errorMessage: string;
  showMessage = false;
  restImg = require('../../../assets/rest.png');
  constructor(private fb: FormBuilder,
              private appService: AppService,
              private router: Router) { }

  ngOnInit() {
    this.validateForm = this.fb.group({
      email: [null, [Validators.required]],
      password: [null, [Validators.required]],
      repasswd: [null, [Validators.required]],
      code: [null, [Validators.required]],
    });
  }
  enterRegister() {
    this.router.navigate(['register']).catch();
  }
  enterLogin() {
    this.router.navigate(['login']).catch();
  }
  restPassword() {
    if (this.validateForm.valid) {
      this.appService.post(this.appService.urls.reset, this.validateForm.value).subscribe((resp: any) => {
        if (resp.ret === 0) {
          this.showMessage = true;
          this.errorMessage = resp.msg;
          timer(2000).subscribe(() => {
            this.showMessage  = false;
          });
        } else {
          this.router.navigate(['login']).catch();
        }
      });
    }
  }
}
